//
//  LGConfirmTableController2.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/27.
//

#import "LGRefundTableController2.h"
#import "LGOrderConfirmTableCell.h"
#import "LGOrderConfirmHeader.h"
#import "LGOrderConfirmFooter.h"
#import "LGOrderConfirmModel.h"
@interface LGRefundTableController2 ()<UITableViewDataSource,UITableViewDelegate,LGOrderConfirmDelegate>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *monthBtn;
/**   */
@property (nonatomic , strong)UIButton *serviceBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *groupType;
/**   */
@property (nonatomic , copy)NSString *pageNo;
/**   */
@property (nonatomic , copy)NSString *keyword;

@end

@implementation LGRefundTableController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNo = @"1";
    self.groupType = @"1";
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.monthBtn];
    [self.view addSubview:self.serviceBtn];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:self.tableView.frame];
    [self requestData];
}

-(void)viewWillAppearAction{
    if ([self.type isEqualToString:@"2"]) {
        [self requestData];
    }
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestDataWithPage:@"1"];
}

-(void)requestMoreData{
    [self requestDataWithPage:self.pageNo];
}

-(void)requestDataWithPage:(NSString *)pageNo{
    __block NSString *currentPage = pageNo;
    self.keyword = (_keyword && ![_keyword isEmptyString])?_keyword:@"";
    self.groupType = (_groupType && ![_groupType isEmptyString])?self.groupType:@"1";
    [RequestUtil withPOST:@"/doctor/order/refundPageList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"2000000",@"order_type":@"10",@"refund_state":@"100",@"client":@"APP",@"data_goup":self.groupType,@"keyword":self.keyword} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if([currentPage isEqualToString:@"1"]){
            [self.dataArray removeAllObjects];
        }
        if(code == 200){
            //总数
            NSInteger total = [LGNSString(returnData[@"total"]) integerValue];
            if (self.delegate && [self.delegate respondsToSelector:@selector(totalNumberChanged:type:)]) {
                [self.delegate totalNumberChanged:total type:self.type];
            }
            
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:[LGOrderConfirmModel mj_objectArrayWithKeyValuesArray:tempArray]];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *lang = textField.textInputMode.primaryLanguage;// 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textField markedTextRange];
        if (!selectedRange) { // 没有高亮
            self.keyword = textField.text;
            [self requestData];
        }
    }else{
        self.keyword = textField.text;
        [self requestData];
    }
}

-(void)selectTypeAction:(UIButton *)sender{
    if (sender.selected == YES) return;
    sender.selected = YES;
    if (sender == self.monthBtn) {
        self.groupType = @"1";
        self.serviceBtn.selected = NO;
        
    }else{
        self.groupType = @"2";
        self.monthBtn.selected = NO;
    }
    [self requestData];
}

//展开 、 收起
-(void)openOrCloseSection:(NSInteger)section model:(LGOrderConfirmModel *)model{
    model.opened = !model.opened;
    [self.dataArray replaceObjectAtIndex:section withObject:model];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LGOrderConfirmModel *model = self.dataArray[section];
    if (model.opened == YES && model.lists) {
        return model.lists.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGOrderConfirmModel *model = self.dataArray[indexPath.section];
    LGOrderConfirmTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = model.lists[indexPath.row];
    cell.showRefund = YES;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(45);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGOrderConfirmHeader *headerView = [[LGOrderConfirmHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(45))];
    headerView.model = self.dataArray[section];
    headerView.section = section;
    headerView.delegate = self;
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return view;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(55), Screen_W, Screen_H-topBarHeight-viewPix(85)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGOrderConfirmTableCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = viewPix(180);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(8), Screen_W-viewPix(232), viewPix(34))];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textColor = [UIColor colorWithString:@"#555555"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"输入患者姓名";
        _searchTF.cornerRidus = viewPix(16);
        _searchTF.font = LGFont(15);
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = [self leftView];
    }
    return _searchTF;
}

-(UIView *)leftView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(35), viewPix(34))];
    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(9), viewPix(16), viewPix(16))];
    iconView.image = [UIImage imageNamed:@"searchGray16"];
    [baseView addSubview:iconView];
    return baseView;
}

-(UIButton *)monthBtn{
    if(!_monthBtn){
        _monthBtn = [UIButton buttonWithTitle:@"按月份显示" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#4C87FD"];
        _monthBtn.frame = CGRectMake(Screen_W-viewPix(200), viewPix(5), viewPix(80), viewPix(40));
        _monthBtn.selected = YES;
        [_monthBtn addTarget:self action:@selector(selectTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _monthBtn;
}

-(UIButton *)serviceBtn{
    if(!_serviceBtn){
        _serviceBtn = [UIButton buttonWithTitle:@"按服务包显示" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#4C87FD"];
        _serviceBtn.frame = CGRectMake(Screen_W-viewPix(110), viewPix(5), viewPix(100), viewPix(40));
        [_serviceBtn addTarget:self action:@selector(selectTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _serviceBtn;
}

@end
